package com.gm.commonlib.network;



import com.gm.commonlib.application.MyApplicationHolder;
import com.gm.commonlib.network.basenetwork.BaseHttpClient;
import com.gm.commonlib.network.basenetwork.RetrofitHelper;
import com.gm.commonlib.network.basenetwork.config.HttpOptions;
import com.gm.commonlib.network.basenetwork.interfaces.IGetHttpBaseUrl;

import java.io.File;

public class BaseHttpClientManger {
    private static BaseHttpClientManger baseHttpClientManger;

    public static BaseHttpClientManger getBaseHttpClientManger() {
        if (baseHttpClientManger == null) {
            baseHttpClientManger = new BaseHttpClientManger();
        }
        return baseHttpClientManger;
    }


    public void initHttpClient(HttpOptions.Builder builder) {

        BaseHttpClient.getHttpclient().init(getHttpOptions(builder), new IGetHttpBaseUrl() {
            @Override
            public String getBaseUrl() {
                String baseUrl = "";
                if (MyApplicationHolder.getApplication() != null) {
                    baseUrl = MyApplicationHolder.getApplication().getBaseUrl();
                }
                return baseUrl + "/";
            }
        });
    }

    public void initHttpClient(HttpOptions.Builder options, IGetHttpBaseUrl iGetHttpBaseUrl) {
        BaseHttpClient.getHttpclient().init(options, iGetHttpBaseUrl);
    }

    public HttpOptions.Builder getHttpOptions(HttpOptions.Builder options) {
        if (MyApplicationHolder.getApplication() != null) {
            File cacheFile = new File(MyApplicationHolder.getApplication().getContext().getExternalCacheDir(), "responses");
            options.setCacheFile(cacheFile);
        }
        options.setInterceptor(RetrofitHelper.getHttpLoggingInterceptor());
        return options;
    }


}
